package com.dlwx.firefighting.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResetPassWordActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.edit_oldpsw)
    EditText editOldpsw;
    @BindView(R.id.edit_newpsw)
    EditText editNewpsw;
    @BindView(R.id.edit_surepsw)
    EditText editSurepsw;
    @BindView(R.id.btn_commit_password)
    TextView btnCommitPassword;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_pass_word);
        ButterKnife.bind(this);
        userId= (String) SPUtils.get(this,Constant.USER_ID,"");
        initView();
    }

    private void initView() {
        tvMiddleTitle.setText("修改密码");
        titleLeftOneBtn.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.title_left_one_btn, R.id.btn_commit_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                ResetPassWordActivity.this.finish();
                break;
            case R.id.btn_commit_password:
                String oldPsw=editOldpsw.getText().toString();
                String newPsw=editNewpsw.getText().toString();
                String surePsw=editSurepsw.getText().toString();
                if(TextUtils.isEmpty(oldPsw)||TextUtils.isEmpty(newPsw)||TextUtils.isEmpty(surePsw)){
                    ToastUtils.toastForShort(ResetPassWordActivity.this,"密码不能为空");
                    return;
                }
                if(!TextUtils.equals(newPsw,surePsw)){
                    ToastUtils.toastForShort(ResetPassWordActivity.this,"新密码不一致！请重新输入。");
                    return;
                }
                resetPsw(oldPsw,newPsw);
                break;
        }
    }
    private void resetPsw(String oldPsw,String newPsw){
        OkGo.<String>post(Constant.USER)
                .tag(this)
                .params("user_id", userId)
                .params("action", "user_info_pwd")
                .params("ori_pwd", oldPsw)
                .params("new_pwd", newPsw)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DealResult dealResult = new DealResult();
//                        BaseBean bean = dealResult.dealBaseRes(ResetPassWordActivity.this, response);
//                        if(bean!=null){
//                            if(bean.getStatus()==Constant.SUCESS){
//                                ToastUtils.toastForShort(ResetPassWordActivity.this,bean.getMsg());
//                                ResetPassWordActivity.this.finish();
//                            }else{
//                                ToastUtils.toastForShort(ResetPassWordActivity.this,bean.getMsg());
//                            }
//                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(ResetPassWordActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }
}
